<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/8
 * Time: 14:15
 */

namespace app\admin\controller;


use app\reuse\controller\ResponseJson;
use think\App;
use think\Db;

class PlayerData extends Common
{
    use ResponseJson;


    public function __construct(App $app = null)
    {
        parent::__construct();
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 年龄分析
     */
    function ageAnalysis()
    {
        $info = Db::table('member')
            ->field('count(id) countNum,
            (select count(id) from member where age <20) num0,
            (select count(id) from member where age >=20 and age <30) num1,
            (select count(id) from member where age >=30 and age <40) num2,
            (select count(id) from member where age >=40 and age <50) num3,
            (select count(id) from member where age >=50 and age <60) num4,
            (select count(id) from member where age >=60) num5
            ')
            ->find();
        $this->assign('data', implode(',', array($info['num0'], $info['num1'], $info['num2'], $info['num3'], $info['num4'], $info['num5'])));
        $this->assign('countNum', $info['countNum']);
        $this->assign('num0', $info['num0']);
        $this->assign('num1', $info['num1']);
        $this->assign('num2', $info['num2']);
        $this->assign('num3', $info['num3']);
        $this->assign('num4', $info['num4']);
        $this->assign('num5', $info['num5']);
        return $this->fetch('/playerData/ageAnalysis');
    }

    /**
     * @return mixed|string
     * @throws \think\exception\DbException
     * @content 区域分布
     */
    function regionalDistribution()
    {
        if (input('get.action') != 'ajax') {
            return $this->fetch('/playerData/regionalDistribution');
        } else {
            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            $_where = 'id >0';
            # 搜索条件
            $searchParams = json_decode(input('post.searchParams'), true);
            # 地区筛选
            if (!empty($searchParams['province'])) {
                $_where .= " and province='" . $searchParams['province'] . "'";
            }
            if (!empty($searchParams['city'])) {
                $_where .= " and city='" . $searchParams['city'] . "'";
            }
            if (!empty($searchParams['area'])) {
                $_where .= " and area='" . $searchParams['area'] . "'";
            }
            $info = Db::table('member')
                ->field('province,city,area,address,count(id) countNum')
                ->where($_where)
                ->group('address')
                ->order('countNum desc')
                ->paginate($listRows, false, ['query' => request()->param()]);
            return $this->jsonSuccessData($info);
        }
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 玩家等级分析统计
     */
    function playerLevelAnalysis()
    {
        $info = Db::table('member m')
            ->field('count(m.id) countNum,ml.title')
            ->join('member_level ml', 'ml.level=m.level', 'right')
            ->group('ml.level')
            ->order('ml.level asc')
            ->select();
        $num = array_column($info, 'countNum');
        $this->assign('data', implode(',', $num));
        $this->assign('title', implode(',', array_map('change_to_quotes', array_column($info, 'title'))));
        $this->assign('countNum', array_sum(array_column($info, 'countNum')));
        return $this->fetch('/playerData/playerLevelAnalysis');
    }
}